package server;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;

import network.Buddy;
import network.Group;
import network.Status;

public class User implements Serializable, Cloneable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -347722471748818629L;

	private HashMap <String, Buddy> buddies;
	private HashMap <String, Group> groups;
	private String password;
	private Buddy me;

	public User(String name, Status st, String pass){
		this(name,st);
		this.password = pass;
	}

	public User(String name, Status st){
		me = new Buddy(name,st);
		buddies = new HashMap<String, Buddy>();
		groups = new HashMap<String, Group> ();
		password = "";
	}

	public void addGroup(Group g){
		groups.put(g.getName(),g);
	}

	public void addBuddy(Buddy b){
		buddies.put(b.getName(),b);
	}

	public LinkedList<Object> getBuddies() {
		LinkedList<Object> aux = new LinkedList<Object>(buddies.values());
		return aux;
	}

	public Buddy getBuddy(String name){
		return buddies.get(name);
	}

	public LinkedList<Object> getGroups() {
		LinkedList<Object> aux = new LinkedList<Object>(groups.values());
		return aux;
	}

	public String getPassword() {
		return password;
	}

	public Status getStatus() {
		return me.getStatus();
	}

	public Group getGroup(String name){
		return groups.get(name);
	}

	public String getUserName() {
		return me.getName();
	}

	public boolean hasBuddy(String name){
		return buddies.containsKey(name);
	}

	public boolean hasGroup(String name){
		return groups.containsKey(name);
	}

	public void removeBuddy(String name){
		buddies.remove(name);

		Collection <Group> aux = groups.values();
		groups=new HashMap<String, Group>();

		for(Group gr : aux){
			gr.removeBudyinGroup(name);
			groups.put(gr.getName(), gr);
		}
	}

	public void removeGroup(String groupName){
		groups.remove(groupName);
	}

	public void setStatus(Status status) {
		me.setStatus(status);
	}
	
	public void setAllBuddiesStatus(Status st){
		Collection <Buddy> aux = buddies.values();
		buddies=new HashMap<String, Buddy>();
		
		for(Buddy b : aux){
			b.setStatus(st);
			buddies.put(b.getName(), b);
		}
	}

	public Buddy toBuddy(){
		return me;
	}

	public void updateBuddy(Buddy b){
		if(hasBuddy(b.getName())){
			buddies.remove(b);
			buddies.put(b.getName(),b);

			Collection <Group> aux = groups.values();
			groups=new HashMap<String, Group>();

			for(Group gr : aux){
				gr.updateBuddyInGroup(b);
				groups.put(gr.getName(), gr);
			}
		}
	}

	public void updateGroup(Group g){
		groups.remove(g);
		groups.put(g.getName(),g);
	}

	public Object clone() {
		Buddy b = (Buddy) me.clone();
		return new User(b.getName(), b.getStatus(), new String(password));
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof User){
			User other = (User) obj;
			return getUserName().equals(other.getUserName());
		}else{
			return me.equals(obj);
		}
	}

	@Override
	public String toString() {
		if(this==null) return null;
		return "[" + me.getName() + "-" + password + "] => " + me.getStatus() + 
			"\n:: " + buddies.toString();
	}


}
